package com.ruoyi.web.controller.quality;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.quality.domain.bo.QualityPhysicalDefectBo;
import com.ruoyi.quality.domain.vo.QualityPhysicalDefectVo;
import com.ruoyi.quality.domain.vo.RecentHalfYearDefectVo;
import com.ruoyi.quality.service.IQualityPhysicalDefectService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 质量问题
 *
 * @author ruoyi
 * @date 2023-07-31
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/quality/physicalDefect")
public class QualityPhysicalDefectController extends BaseController {

    private final IQualityPhysicalDefectService iQualityPhysicalDefectService;

    /**
     * 查询质量问题列表
     */
    @SaCheckPermission("quality:physicalDefect:list")
    @GetMapping("/getRecentHalfYearDefect")
    public List<RecentHalfYearDefectVo> getRecentHalfYearDefect(QualityPhysicalDefectBo bo) {
        return iQualityPhysicalDefectService.getRecentHalfYearDefect(bo);
    }

    /**
     * 查询质量问题列表
     */
    @SaCheckPermission("quality:physicalDefect:list")
    @GetMapping("/list")
    public TableDataInfo<QualityPhysicalDefectVo> list(QualityPhysicalDefectBo bo, PageQuery pageQuery) {
        return iQualityPhysicalDefectService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出质量问题列表
     */
    @SaCheckPermission("quality:physicalDefect:export")
    @Log(title = "质量问题", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(QualityPhysicalDefectBo bo, HttpServletResponse response) {
        List<QualityPhysicalDefectVo> list = iQualityPhysicalDefectService.queryList(bo);
        ExcelUtil.exportExcel(list, "质量问题", QualityPhysicalDefectVo.class, response);
    }

    /**
     * 导出选中项质量问题列表
     */
    @SaCheckPermission("quality:physicalDefect:export")
    @Log(title = "质量问题", businessType = BusinessType.EXPORT)
    @PostMapping("/export/{defectIds}")
    public void exportByIds(@PathVariable Long[] defectIds, HttpServletResponse response) {
        List<QualityPhysicalDefectVo> list = iQualityPhysicalDefectService.queryByIds(Arrays.asList(defectIds));
        ExcelUtil.exportExcel(list, "质量问题", QualityPhysicalDefectVo.class, response);
    }

    /**
     * 获取质量问题详细信息
     *
     * @param defectId 主键
     */
    @SaCheckPermission("quality:physicalDefect:query")
    @GetMapping("/{defectId}")
    public R<QualityPhysicalDefectVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long defectId) {
        return R.ok(iQualityPhysicalDefectService.queryById(defectId));
    }

    /**
     * 新增质量问题
     */
    @SaCheckPermission("quality:physicalDefect:add")
    @Log(title = "质量问题", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody QualityPhysicalDefectBo bo) {
        return toAjax(iQualityPhysicalDefectService.insertByBo(bo));
    }

    /**
     * 修改质量问题
     */
    @SaCheckPermission("quality:physicalDefect:edit")
    @Log(title = "质量问题", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody QualityPhysicalDefectBo bo) {
        return toAjax(iQualityPhysicalDefectService.updateByBo(bo));
    }

    /**
     * 删除质量问题
     *
     * @param defectIds 主键串
     */
    @SaCheckPermission("quality:physicalDefect:remove")
    @Log(title = "质量问题", businessType = BusinessType.DELETE)
    @DeleteMapping("/{defectIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] defectIds) {
        return toAjax(iQualityPhysicalDefectService.deleteWithValidByIds(Arrays.asList(defectIds), true));
    }
}
